<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/images/IMG58.jpg"
             alt="">
        <span>电商管理平台</span>

      </div>
      <el-button type="info"
                 @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle_botton"
             @click="toggleCollp">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu :unique-opened="true"
                 background-color="#333743"
                 text-color="#fff"
                 active-text-color="#409bef"
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="activePath">
          <el-submenu :index="item.id+''"
                      v-for="item in menulist"
                      :key='item.id'>
            <template slot="title">
              <i :class="elIcon[item.id]"></i>
              <span>{{item.authName}}

              </span>
            </template>

            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="active('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </el-menu-item>

          </el-submenu>

        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
      </el-main>

    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      elIcon: {
        125: 'el-icon-s-custom',
        103: 'el-icon-watermelon',
        101: 'el-icon-s-goods',
        102: 'el-icon-document',
        145: 'el-icon-date'
      },
      menulist: [],
      isCollapse: false,
      // 被激活的链接
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 保存链接的激活状态
    active (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    // 获取所有菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      console.log(res.data)
    },
    toggleCollp () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  padding-left: 0;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    font-size: 20px;
    height: 100%;
    img {
      height: 100%;
    }
    span {
      padding-left: 10px;
    }
  }
  > .el-button {
    background-color: #909399;
  }
}
.el-header,
.el-footer {
  background-color: #28292b;
  color: white;
  text-align: center;
  line-height: 60px;
}
.el-menu li {
  background-color: none;
}

.el-aside {
  background-color: #333744;
  color: white;
  text-align: center;
  line-height: 200px;
  .el-menu {
    border-right: none;
  }
  .toggle_botton {
    background: #545763;
    font-size: 12px;
    line-height: 26px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}

.el-main {
  background-color: #f1f1f1;
  color: #333;
  text-align: center;
}
</style>
